# -*- coding: UTF-8 -*-
# __author__ = '10459'


class MyResource:
    # def __enter__(self):
    #     print ("connect to resource")
    #     return self
    # def __exit__(self, exc_type, exc_val, exc_tb):
    #     print("close resource")
    def query(self):
        print("query data")


# 要用with语句，一个需要上下文管理器，就是_enter_与_exit_方法需要
# with MyResource() as r:
#     r.query()
from contextlib import contextmanager  # 用contextmanager简化上下文管理器的————enter————与——exit——


@contextmanager
def make_myresource():
    print("connect to resource", "ddddf")
    yield MyResource()  # 函数中有return,执行后不会返回来执行，用yield可以
    print("close resource")


with make_myresource()  as e:
    e.query()
